/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.bsly.modules.sys.controller;

import io.bsly.common.annotation.SysLog;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.common.validator.Assert;
import io.bsly.modules.app.annotation.JwtIgnore;
import io.bsly.modules.app.entity.localEntity.UserEntity;
import io.bsly.modules.app.pojo.UserFrom;
import io.bsly.modules.app.service.localmaster.UserService;
import io.bsly.modules.sys.entity.SysUserEntity;
import io.bsly.modules.sys.pojo.PasswordForm;
import io.bsly.modules.sys.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统用户
 */
@Api(tags = "后台sys用户管理")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	UserService userService;

	/**
	 * 所有用户列表
	 */
	@ApiOperation("后台用户分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page",value = "当前页数" , dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "username",value = "用户名" , dataType = "string", paramType = "query")
	})
	@GetMapping("/sysUserPage")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysUserService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 获取用户列表
	 */
	@ApiOperation("C端用户分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page",value = "当前页数" , dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "userId",value = "用户id" , dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "userName",value = "用户名" , dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "registerDate",value = "注册时间" , dataType = "string", paramType = "query",example = "2021-09-24")
	})
	@GetMapping("/dyUserPage")
	public R list(UserFrom userFrom){
		PageUtils page =  userService.queryPage(userFrom);
		return R.ok().put("page", page);
	}


    /**
     * C端用户修改
     */
    @ApiOperation("C端用户根据Id修改")
    @PostMapping("/dyUserUpdate")
    public R update(@RequestBody UserEntity userEntity){
        userService.updateById(userEntity);
        return R.ok();
    }

	/**
	 * 获取登录的用户信息
	 */
	@ApiOperation("查询")
	@GetMapping("/info/{id}")
	public R infoById(@PathVariable("id") @ApiParam Integer id){
		SysUserEntity userEntity = sysUserService.getById(id);
		return R.ok().put("userEntity", userEntity);
	}

	
	/**
	 * 修改登录用户密码
	 */
	@ApiOperation("修改密码")
	@PostMapping("/password")
	public R password(@RequestBody PasswordForm form){
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");
		//sha256加密
		String password = DigestUtils.sha256Hex(form.getPassword());
		//sha256加密
		String newPassword = DigestUtils.sha256Hex(form.getNewPassword());
		//更新密码
		boolean flag = sysUserService.updatePassword(form.getUserId(), password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}
		return R.ok();
	}
	

	
	/**
	 * 保存用户
	 */
	@JwtIgnore
	@ApiOperation("保存")
	@PostMapping("/save")
	public R save(@RequestBody SysUserEntity user){
		sysUserService.saveUser(user);
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@ApiOperation("后台用户修改")
	@PostMapping("/sysUpdate")
	public R update(@RequestBody SysUserEntity user){
		sysUserService.update(user);
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Integer[] userIds){
		if(ArrayUtils.contains(userIds, 1L)){
			return R.error("系统管理员不能删除");
		}
		sysUserService.deleteBatch(userIds);
		return R.ok();
	}
}
